package com.netbetter.langzheng.client.mvc;

import com.google.gwt.user.client.ui.RootPanel;

import com.extjs.gxt.ui.client.Registry;
import com.extjs.gxt.ui.client.Style.LayoutRegion;
import com.extjs.gxt.ui.client.mvc.AppEvent;
import com.extjs.gxt.ui.client.mvc.Controller;
import com.extjs.gxt.ui.client.mvc.View;
import com.extjs.gxt.ui.client.util.Margins;
import com.extjs.gxt.ui.client.widget.LayoutContainer;
import com.extjs.gxt.ui.client.widget.Viewport;
import com.extjs.gxt.ui.client.widget.layout.BorderLayout;
import com.extjs.gxt.ui.client.widget.layout.BorderLayoutData;
import com.extjs.gxt.ui.client.widget.layout.FitLayout;
import com.netbetter.langzheng.commons.constants.AppEvents;

/**
 * 整个应用视图
 * 
 * @author 嘉陵江里捉鱼
 * 
 */
public class AppView extends View {

  private Viewport viewport;
  private LayoutContainer west;
  private LayoutContainer center;

  public AppView(Controller controller) {
    super(controller);
  }

  protected void initialize() {

  }

  private void initUI() {
    viewport = new Viewport();
    viewport.setLayout(new BorderLayout());

    createWest();
    createCenter();

    // registry serves as a global context
    Registry.register("viewport", viewport);
    Registry.register("west", west);
    Registry.register("center", center);

    RootPanel.get().add(viewport);
  }

  private void createWest() {
    BorderLayoutData data = new BorderLayoutData(LayoutRegion.WEST, 300, 150,
        550);
    data.setMargins(new Margins(3, 0, 3, 3));
    data.setCollapsible(true);
    west = new LayoutContainer();

    viewport.add(west, data);
  }

  private void createCenter() {
    center = new LayoutContainer();
    center.setLayout(new FitLayout());

    BorderLayoutData data = new BorderLayoutData(LayoutRegion.CENTER);
    data.setMargins(new Margins(3, 3, 3, 3));

    viewport.add(center, data);
  }

  protected void handleEvent(AppEvent event) {
    switch (event.type) {
      case AppEvents.Init:
        initUI();
        break;

    }
  }

}
